(: 
Part 2 / Q7
An XML document (satisfying the DTD in file distribution.dtd) that contains
the number of students who got 0 answers correct on the quiz, 1 correct, and
so on, up to the maximum possible.
:)

(: 
Return the number of questions $student answered correctly, according to the
answers stored in $bank.
:)
declare function local:student_correct($student, $bank)
{
    count(
        for $q in $student/QuestionResponse 
        let $qid := data($q/@QID)
        let $correct_ans := data($bank/Question/*[@QID eq $qid]/@answer) 
        return if (data($q/@Response) eq $correct_ans) then "x" else ()
    )
};

(: -- Query -- :)
<Distribution>
{
let $num_correct_per_student := 
    for $s in fn:doc("class.xml")/ClassResponses/Student
    return local:student_correct($s, fn:doc("bank.xml")/QuestionBank)
let $num_questions := count(fn:doc("quiz.xml")//Question)
for $n in 0 to $num_questions
    return 
        (<Bar count="{$n}"
        numcorrect="{count($num_correct_per_student[. eq $n])}" />)
}
</Distribution>
